import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QVBoxLayout, QWidget, QLabel, QMenu, QAction
from PyQt5.QtCore import Qt  # 确保Qt模块被导入以使用Qt.AlignCenter

class CustomMainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.initUI()

    def initUI(self):
        # 设置窗口标题
        self.setWindowTitle('水库与建筑物GUI')

        # 创建中心窗口部件
        central_widget = QWidget()
        self.setCentralWidget(central_widget)

        # 创建主布局
        main_layout = QVBoxLayout()
        central_widget.setLayout(main_layout)

        # 初始化显示区域（用于显示“水库”或“建筑物”文字）
        self.display_label = QLabel('', alignment=Qt.AlignCenter)
        main_layout.addWidget(self.display_label)

        # 创建菜单栏
        menu_bar = self.menuBar()

        # 添加水库菜单
        reservoir_menu = menu_bar.addMenu('水库')
        reservoir_action = QAction('显示水库', self)
        reservoir_action.triggered.connect(lambda: self.showText('水库'))
        reservoir_menu.addAction(reservoir_action)

        # 添加建筑物菜单
        building_menu = menu_bar.addMenu('建筑物')
        building_action = QAction('显示建筑物', self)
        building_action.triggered.connect(lambda: self.showText('建筑物'))
        building_menu.addAction(building_action)

        # 创建按钮
        reservoir_button = QPushButton('水库')
        reservoir_button.clicked.connect(lambda: self.showText('水库'))
        main_layout.addWidget(reservoir_button)

        building_button = QPushButton('建筑物')
        building_button.clicked.connect(lambda: self.showText('建筑物'))
        main_layout.addWidget(building_button)

    def showText(self, text):
        """更新显示区域的文字"""
        self.display_label.setText(text)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = CustomMainWindow()
    window.show()
    sys.exit(app.exec_())